import 'package:flutter/material.dart';
import '../model/CommonModel.dart';
import '../common/Constants.dart';
import 'package:dio/dio.dart';
import 'WebViewPage.dart';
import 'dart:convert';

// 文章列表页
// ignore: must_be_immutable, must_be_immutable
class EssayListPage extends StatefulWidget {
  String title;
  String path;
  String tag;

  EssayListPage({this.title, this.tag, this.path});

  @override
  State<StatefulWidget> createState() {
    return BlipState();
  }
}

class BlipState extends State<EssayListPage> {
  int _curPage = 1;
  String _pageId = "0";
  List<dynamic> list = [];
  bool _isLoading = false;
  ScrollController _controller = new ScrollController();

  BlipState() {
    _controller.addListener(() {
      var maxScroll = _controller.position.maxScrollExtent;
      var pixels = _controller.position.pixels;
      if (maxScroll == pixels) {
        print("加载更多...");
        loadNetworkData(widget.tag, _curPage, _pageId);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    if (widget.path == null) {
      loadNetworkData(widget.tag, _curPage, _pageId);
    } else {
      loadLocalData(widget.path);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff313131),
        title: Text(
          widget.title,
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: ListView.builder(
          itemCount: list.length,
          itemBuilder: (BuildContext context, int index) {
            return InkWell(
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) =>
                            WebViewPage(
                              title: (list[index] as Datas).title,
                              url: (list[index] as Datas).html5,
                              id: (list[index] as Datas).id,
                            )));
              },
              child: Column(
                children: <Widget>[
                  Container(
                    height: 100,
                    padding: EdgeInsets.all(15),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          width: 100,
                          height: 70,
                          child: FadeInImage.assetNetwork(
                            placeholder: Constants.LOCAL_IMAGE,
                            image: (list[index] as Datas).thumbnail,
                            fit: BoxFit.cover,
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 20),
                          child: Column(
                            mainAxisSize: MainAxisSize.max,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Expanded(
                                  flex: 2,
                                  child: Container(
                                      width: 155,
                                      child: Text(
                                        (list[index] as Datas).title,
                                        maxLines: 2,
                                        style: TextStyle(fontSize: 20),
                                      ))),
                              Padding(
                                padding: EdgeInsets.only(bottom: 15),
                              ),
                              Expanded(
                                  flex: 1,
                                  child: Text((list[index] as Datas).author,
                                      textAlign: TextAlign.start,
                                      style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 15)))
                            ],
                          ),
                        ),
                        Expanded(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                Image.asset("image/indiimg_9x17_@2x.png",
                                    width: 18, height: 18)
                              ],
                            ))
                      ],
                    ),
                  ),
                  Divider(
                    height: 1,
                    color: Colors.black26,
                  )
                ],
              ),
            );
          }
          ),
    );
  }

  // 加载本地数据
  void loadLocalData(String path) async {
    var str = await DefaultAssetBundle.of(context).loadString(path);
    var data = CommonModel.fromJson(json.decode(str));
    setState(() {
      print("size：${data.datas.length}");
      list = data.datas;
    });
  }

  // 加载网络数据
  void loadNetworkData(String tag, int curPage, String pageId) async {
    Dio dio = new Dio();
    Response response = await dio.get(
        "http://203.195.230.211/index.php?m=Home&c=Api2&a=getTagList&tag=$tag&p=$curPage&client=iOS&show_sdv=1&page_id=$pageId&create_time=0&sign=24a904d18a786327741ca5613180ceda&time=1547092560&device_id=A39632E7-F689-405B-A3A3-0319D6095B54&version=1.6.2&client=iOS");
    print("data：${response.data}");
    CommonModel model = CommonModel.fromJson(response.data);
    setState(() {
      _curPage++;
      print("size：${list.length}");
      this.list.addAll(model.datas);
      _pageId = model.datas[list.length - 1].id;
    });
  }
}
